package org.filehanding.controller;

import org.filehanding.entity.FileHanding;
import org.filehanding.entity.User;
import org.filehanding.service.FileService;
import org.filehanding.util.PropertiesUtil;
import org.filehanding.vo.ResponseValue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.net.URLEncoder;
import java.util.List;


/**
 * Created by liguo on 2016/5/14.
 */
@RestController
public class FileController extends BaseController {
    @Autowired
    private FileService fileService;

    /**
     * 文件上传
     *
     * @param file
     * @param session
     * @return
     */
    @RequestMapping(value = "/upload")
    public String upload(MultipartFile file, HttpSession session) {
        User user = (User) session.getAttribute("loginUser");
        fileService.addfile(file, user);
        return "1";
    }

    /**
     * 获取上传文件列表
     *
     * @param session
     * @return
     */
    @RequestMapping(value = "/files", method = RequestMethod.GET)
    public ResponseValue getFiles(HttpSession session) {
        User user = (User) session.getAttribute("loginUser");
        List<FileHanding> files = fileService.getByUserGuid(user.getGuid());
        ResponseValue responseValue = new ResponseValue();
        responseValue.setCode(ResponseValue.RESPONSE_CODE_SUCCESS);
        responseValue.setResult(files);
        return responseValue;
    }

    /**
     * 获取文件下载列表
     * @param session
     * @return
     */
    @RequestMapping(value = "/donwload/files", method = RequestMethod.GET)
    public ResponseValue getdownloadFiles(HttpSession session) {
        User user = (User) session.getAttribute("loginUser");
        List<FileHanding> files = fileService.listDownloadFiles(user);
        ResponseValue responseValue = new ResponseValue();
        responseValue.setCode(ResponseValue.RESPONSE_CODE_SUCCESS);
        responseValue.setResult(files);
        return responseValue;
    }

    /**
     * 下载文件
     *
     * @param fileName
     * @return
     */
    @RequestMapping(value = "/download/file", method = RequestMethod.GET)
    public void getFile(String fileName,
                        HttpServletResponse response,
                        HttpSession session) throws Exception {
        User user = (User) session.getAttribute("loginUser");
        String basePath = PropertiesUtil.getFileDownloadPath()+File.separator+user.getName();
        File file = new File(basePath, fileName);
        InputStream fis = new BufferedInputStream(new FileInputStream(file));

        OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
        try {
            response.reset();
            response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName, "utf-8"));
            response.setContentType("application/octet-stream; charset=utf-8");
            response.addHeader("Content-Length", "" + file.length());
            byte[] b = new byte[1024];
            int len;
            while ((len = fis.read(b)) > 0)
                response.getOutputStream().write(b, 0, len);
            fis.close();
            toClient.flush();
            toClient.close();
        } finally {
            if (toClient != null) {
                toClient.close();
            }
        }
    }
}
